package com.pfizer.PicView;

import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.webkit.URLUtil;

public class AsyncImageLoader {
	private Map<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>();

	public Drawable loadDrawable(final String imageUrl, final int loadType,
			final ImageCallback callback) {
		if (imageUrl.length() == 0) {
			return null;
		}

		if (imageCache.containsKey(imageUrl)) {
			SoftReference<Drawable> softReference = imageCache.get(imageUrl);
			if (softReference.get() != null) {
				return softReference.get();
			}
		}

		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				callback.imageLoaded((Drawable) msg.obj, imageUrl);
			}
		};
		// load data
		new Thread() {
			public void run() {
				Drawable drawable = loadImageFromUrl(imageUrl, loadType);
				imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
				handler.sendMessage(handler.obtainMessage(0, drawable));
			};
		}.start();

		return null;
	}

	/*
	 * protected Drawable loadImageFromUrl(String imageUrl) { try { return
	 * Drawable.createFromStream(new URL(imageUrl).openStream(), "src"); } catch
	 * (Exception e) { throw new RuntimeException(e); } }
	 */
	protected Drawable loadImageFromUrl(String imageUrl, int loadType) {
		Bitmap bitmap = null;
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = false;
		options.inSampleSize = 2; // width��hight��Ϊԭ����ʮ��һ
		if (imageUrl != null) {
			try {
				if (URLUtil.isHttpUrl(imageUrl)) {// ���Ϊ�����ַ��������url����ͼƬ
					URL url = new URL(imageUrl);
					HttpURLConnection conn = (HttpURLConnection) url
							.openConnection();
					conn.setDoInput(true);
					conn.connect();
					InputStream stream = conn.getInputStream();
					switch (loadType) {
					case 0:
						bitmap = BitmapFactory.decodeStream(stream, null, null);
						break;
					case 1:
						bitmap = ImageUtil.DrBitmap(BitmapFactory.decodeStream(
								stream, null, options));
						break;
					}
					stream.close();
				} else {// ���Ϊ������ݣ�ֱ�ӽ���
					// drawable =
					// Drawable.createFromPath(view.getTag().toString());
					switch (loadType) {
					case 0:
						bitmap = BitmapFactory.decodeFile(imageUrl, options);
						break;
					case 1:
						try {
							//Bitmap bit = BitmapFactory.decodeFile(imageUrl,	options);
							//bitmap = Bitmap.createScaledBitmap(bit, 80, 80,true);// 预先缩放，避免实
							//bit.recycle();
							bitmap = BitmapFactory.decodeFile(imageUrl,	options);
						} catch (RuntimeException e) {
							// TODO: handle exception
							return null;
						}
						break;
					}
				}
				options = null;
				return new BitmapDrawable(bitmap);
			} catch (Exception e) {
				return null;
			}
		} else {
			return null;
		}
	}

	// call back interface
	public interface ImageCallback {
		public void imageLoaded(Drawable imageDrawable, String imageUrl);
	}

}
